// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.dbmanager.ui;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dengues.core.metadata.DatabaseContainer;
import org.dengues.core.metadata.MetadataColumn;
import org.dengues.core.metadata.MetadataDatabase;
import org.dengues.core.metadata.MetadataTable;
import org.dengues.warehouse.dbmanager.ui.provider.DatabaseNavigateProvider;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Tree;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-11-25 qiang.zhang $
 * 
 */
public class DatabaseNavigateTree extends Composite {

    private DatabaseContainer databaseContainer;

    private TreeViewer viewer;

    private DatabaseManagerDialog sqlQueryDialog;

    private final Connection conn;

    /**
     * Qiang.Zhang.Adolf@gmail.com DatabaseNavigate constructor comment.
     * 
     * @param parent
     * @param style
     */
    public DatabaseNavigateTree(Composite parent, Connection conn) {
        super(parent, SWT.BORDER);
        this.conn = conn;
        setBackground(parent.getBackground());
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginBottom = 0;
        gridLayout.marginWidth = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginHeight = 0;

        setLayout(gridLayout);

        GridData gridData = new GridData(GridData.FILL_VERTICAL);
        gridData.widthHint = 150;
        setLayoutData(gridData);
        initComposite();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "initComposite".
     */
    private void initComposite() {
        createTitle();
        createNavigateTree();
        addListeners();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addListeners".
     */
    private void addListeners() {
        viewer.addSelectionChangedListener(new ISelectionChangedListener() {

            public void selectionChanged(SelectionChangedEvent event) {
                if (conn == null) {
                    return;
                }
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                Object fe = selection.getFirstElement();
                try {
                    String[] colNames = new String[0];
                    Map<String, List<String[]>> dataMap = new HashMap<String, List<String[]>>();
                    String title = ""; //$NON-NLS-1$
                    if (fe instanceof MetadataDatabase) {
                        colNames = new String[] {
                                Messages.getString("DatabaseNavigateTree.property"), Messages.getString("DatabaseNavigateTree.value") }; //$NON-NLS-1$ //$NON-NLS-2$
                        title = Messages.getString("DatabaseNavigateTree.dataInfo"); //$NON-NLS-1$
                        List<String[]> data = DatabaseManagerUtils.getDatabaseInfo(conn.getMetaData());
                        data.add(0, colNames);
                        dataMap.put(title, data);
                        data = DatabaseManagerUtils.initCatalogs(conn);
                        title = Messages.getString("DatabaseNavigateTree.catalogs"); //$NON-NLS-1$
                        dataMap.put(title, data);
                        title = Messages.getString("DatabaseNavigateTree.connInfo"); //$NON-NLS-1$
                        data = DatabaseManagerUtils.getConnectionInfo(conn.getMetaData());
                        data.add(0, colNames);
                        dataMap.put(title, data);
                    } else if (fe instanceof MetadataTable) {
                        MetadataTable table = (MetadataTable) fe;
                        DatabaseManagerUtils.initDataSet(conn, table.getTableName());
                        sqlQueryDialog.getContentComposite().getDataBrowserComposite().updateDefaultTable(
                                DatabaseManagerUtils.getColumnNames(), DatabaseManagerUtils.getDataSetRowList());
                        dataMap = DatabaseManagerUtils.getTableInfos(conn, null, table.getTableName());
                    } else if (fe instanceof MetadataColumn) {
                        colNames = new String[] {
                                Messages.getString("DatabaseNavigateTree.name"), Messages.getString("DatabaseNavigateTree.value") }; //$NON-NLS-1$ //$NON-NLS-2$
                        List<String[]> data = new ArrayList<String[]>();
                        data.add(0, colNames);
                        MetadataColumn column = (MetadataColumn) fe;
                        title = column.getColumnName() + Messages.getString("DatabaseNavigateTree.info"); //$NON-NLS-1$
                    }
                    sqlQueryDialog.getContentComposite().getObjectBrowserComposite().updateObjectBrowser(dataMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createNavigateTree".
     */
    private void createNavigateTree() {
        viewer = new TreeViewer(this, SWT.H_SCROLL | SWT.V_SCROLL);
        Tree tree = viewer.getTree();
        tree.setLayoutData(new GridData(GridData.FILL_BOTH));
        DatabaseNavigateProvider provider = new DatabaseNavigateProvider();
        viewer.setContentProvider(provider);
        viewer.setLabelProvider(provider);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createTitle".
     */
    private void createTitle() {
        Label title = new Label(this, SWT.NONE);
        title.setText(Messages.getString("DatabaseNavigateTree.Title.Name")); //$NON-NLS-1$
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.heightHint = 18;
        gridData.verticalAlignment = GridData.END;
        title.setAlignment(SWT.CENTER);
        title.setLayoutData(gridData);
    }

    /**
     * Getter for databaseContainer.
     * 
     * @return the databaseContainer
     */
    public DatabaseContainer getDatabaseContainer() {
        return this.databaseContainer;
    }

    /**
     * Sets the databaseContainer.
     * 
     * @param databaseContainer the databaseContainer to set
     */
    public void setDatabaseContainer(DatabaseContainer databaseContainer) {
        this.databaseContainer = databaseContainer;
        viewer.setInput(databaseContainer);
    }

    /**
     * Sets the sqlQueryDialog.
     * 
     * @param sqlQueryDialog the sqlQueryDialog to set
     */
    public void setSqlQueryDialog(DatabaseManagerDialog sqlQueryDialog) {
        this.sqlQueryDialog = sqlQueryDialog;
    }

    /**
     * Getter for viewer.
     * 
     * @return the viewer
     */
    public TreeViewer getViewer() {
        return this.viewer;
    }

}
